package com.wenwan.manage.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonSyntaxException;
import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import com.wenwan.manage.setting.StatusSetting;

/**
 * 异常处理控制
 * 
 * @author Admin
 *
 */
@ControllerAdvice
public class ControllerAdviceAction {


	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Map<String, Object> Exception(Exception e) {
		e.printStackTrace();
		// DataToObject<Object> dto = new DataToObject<Object>();
		// dto.setCode(StatusSetting.CODE_500);
		// dto.setMsg(StatusSetting.MSG_500);
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_500);
		dto.put("code", StatusSetting.CODE_500);
		return dto;
	}

	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	public Map<String, Object> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		// DataToObject<Object> dto = new DataToObject<Object>();
		// dto.setCode(StatusSetting.CODE_405);
		// dto.setMsg(StatusSetting.MSG_405);
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_405);
		dto.put("code", StatusSetting.CODE_405);
		return dto;
	}

	@ExceptionHandler(MissingServletRequestParameterException.class)
	@ResponseBody
	public Map<String, Object> MissingServletRequestParameterException(MissingServletRequestParameterException e) {
		// DataToObject<Object> dto = new DataToObject<Object>();
		// dto.setCode(StatusSetting.CODE_400);
		// dto.setMsg(StatusSetting.MSG_400);
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_400);
		dto.put("code", StatusSetting.CODE_400);
		return dto;
	}

	@ExceptionHandler(JsonSyntaxException.class)
	@ResponseBody
	public Map<String, Object> JsonSyntaxException() {
		// DataToObject<Object> dto = new DataToObject<Object>();
		// dto.setCode(StatusSetting.CODE_10008);
		// dto.setMsg(StatusSetting.MSG_10008);
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_10008);
		dto.put("code", StatusSetting.CODE_10008);
		return dto;
	}

	@ExceptionHandler(MySQLIntegrityConstraintViolationException.class)
	@ResponseBody
	public Map<String, Object> MySQLIntegrityConstraintViolationException() {
		// DataToObject<Object> dto = new DataToObject<Object>();
		// dto.setData(false);
		// dto.setCode(StatusSetting.CODE_10009);
		// dto.setMsg(StatusSetting.MSG_10009);
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_10009);
		dto.put("code", StatusSetting.CODE_10009);
		return dto;
	}
}